//
//  YLPushViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/12/26.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLPushViewModel.h"
#import "AppDelegate.h"
//#import <YYModel.h>
#import "YLPushModel.h"
#import <UIKit/UIKit.h>
#import "LKHomeNoticeDetailViewController.h"

#import "LKWorkViewController.h"
#import "LKWaitHandleOrderViewController.h"
#import "LKWaitHandleCustomerViewController.h"
#import "LKDayInformationViewController.h"


// 引入JPush功能所需头文件
#import <JPUSHService.h>
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
//#import "YLAccountTool.h"

@implementation YLPushViewModel

+ (void)pushWithInfo:(NSDictionary *)info
{
    NSLog(@"我是推送过来的=%@",info);
    YLPushModel *model = [YLPushModel modelWithDictionary:info];
    
    if (model.RedirectType == 0) {
        return;
    }
    
    // 站内跳转
    if (model.RedirectType == 1) {
        if (model.RedirectId == 0) {
            [YLPushViewModel controllerWithIndex:0];
        }
        
        if (model.RedirectId == 11) {
            // 订单列表
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            
            UITabBarController *tabViewController = (UITabBarController *) appDelegate.window.rootViewController;
            
            [tabViewController setSelectedIndex:1];
//            UINavigationController *navvc = tabViewController.viewControllers[1];
//            LKWorkViewController *vc = navvc.viewControllers[0];
//
//            NSLog(@"%@",tabViewController.viewControllers);
//            NSLog(@"%@",vc.lalala);
//            [vc.flipView selectIndex:1];
//            [vc.segment selectIndex:2];
            return;
        }
        
        if (model.RedirectId == 12) {
            // 待处理订单列表
            UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            LKWaitHandleOrderViewController *con = [[LKWaitHandleOrderViewController alloc] init];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [(UINavigationController *)t.selectedViewController pushViewController:con animated:YES];
            });
            
            return;
        }
        
        if (model.RedirectId == 13) {
            //客户列表
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            
            UITabBarController *tabViewController = (UITabBarController *) appDelegate.window.rootViewController;
            
            [tabViewController setSelectedIndex:1];
            UINavigationController *navvc = tabViewController.viewControllers[1];
            LKWorkViewController *vc = navvc.viewControllers[0];
            
            NSLog(@"%@",tabViewController.viewControllers);
            NSLog(@"%@",vc.lalala);
            [vc.flipView selectIndex:1];
            [vc.segment selectIndex:2];
            return;
        }
        
        if (model.RedirectId == 14) {
            // 待处理客户列表
            UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            LKWaitHandleCustomerViewController *con = [[LKWaitHandleCustomerViewController alloc] init];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [(UINavigationController *)t.selectedViewController pushViewController:con animated:YES];
            });
            return;
        }
        
        if (model.RedirectId == 15) {
            // 资讯列表
            UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            LKDayInformationViewController *con = [[LKDayInformationViewController alloc] init];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [(UINavigationController *)t.selectedViewController pushViewController:con animated:YES];
            });
            return;
        }
        if (model.RedirectId == 16) {
            //公告详情
            UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            LKHomeNoticeDetailViewController *con = [[LKHomeNoticeDetailViewController alloc] init];
            con.noticeId = model.RedirectKey;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [(UINavigationController *)t.selectedViewController pushViewController:con animated:YES];
            });
            return;
        }
        return;
    }
    
    if (model.RedirectType == 2) {
        // 外部链接
//        WHWebViewController *webViewController = [[WHWebViewController alloc] init];
//        webViewController.url = model.RedirectKey;
//        UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//        [(UINavigationController *)t.selectedViewController pushViewController:webViewController animated:YES];
        
    }
}

+ (UIViewController *)controllerWithIndex:(NSInteger)index
{
    UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    t.selectedIndex = index;

    UINavigationController *nav =  (UINavigationController *)t.viewControllers[1];
    [nav popToRootViewControllerAnimated:NO];
    
    return nav.viewControllers.firstObject;
}

+ (void)setNewTagsWithInfo:(NSDictionary *)info
{
    YLPushModel *model = [YLPushModel modelWithDictionary:info];
    
    if (model.ContentType == 1 || model.ContentType == -1) {
        
        [JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
            
        } seq:1];
        
        [JPUSHService setTags:[NSSet setWithObject:model.Content] completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
            
        } seq:1];
    }
   
}

+ (void)setUpMyPushInfo
{
    LKUserInfor *account = [LKUserInforTool account];
    
    if (account) {
        [JPUSHService setAlias:account.alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
        } seq:2];
        
        [JPUSHService setTags:[NSSet setWithObject:account.tagAnd] completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
            
        } seq:1];
    }
    
}

+ (void)clearPushInfo
{
    [JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
        
    } seq:1];
    [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
    } seq:2];
    
}

@end
